package com.smart.community.property.controller;

import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.smart.community.commons.result.Result;
import com.smart.community.commons.utils.SecurityUtils;
import com.smart.community.property.service.IPropertyInfoService;
import com.smart.community.property.vo.PropertyInfoVO;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * 物业信息管理Controller
 * @author Wu.Liang
 * @since 2025-01-29
 */
@Slf4j
@RestController
@RequestMapping("/property/info")
@RequiredArgsConstructor
@Validated
@Tag(name = "物业信息管理", description = "物业信息管理相关接口")
public class PropertyInfoController {

    private final IPropertyInfoService propertyInfoService;

    @GetMapping
    @Operation(summary = "查询物业信息", description = "查询当前用户的物业信息")
    public Result<PropertyInfoVO> getPropertyInfo() throws Exception {
        log.info("查询物业信息");
        Result<PropertyInfoVO> result = propertyInfoService.getCurrentUserPropertyInfo();
        log.info("查询物业信息成功");
        return result;
    }

    @GetMapping("/{propertyCompanyId}")
    @Operation(summary = "根据物业公司ID查询物业信息", description = "根据物业公司ID查询对应的物业信息")
    public Result<PropertyInfoVO> getPropertyInfoByCompanyId(
            @Parameter(description = "物业公司ID") @PathVariable Long propertyCompanyId) throws Exception {
        log.info("根据物业公司ID查询物业信息，物业公司ID：{}", propertyCompanyId);
        Result<PropertyInfoVO> result = propertyInfoService.getPropertyInfo(propertyCompanyId);
        log.info("根据物业公司ID查询物业信息成功");
        return result;
    }

    @PutMapping
    @Operation(summary = "更新物业信息", description = "更新当前用户的物业信息")
    public Result<Void> updatePropertyInfo(
            @Parameter(description = "物业信息") @Valid @RequestBody PropertyInfoVO propertyInfoVO) throws Exception {
        log.info("更新物业信息，companyLogo类型: {}, 内容: {}", 
            propertyInfoVO.getCompanyLogo() != null ? propertyInfoVO.getCompanyLogo().getClass().getSimpleName() : "null",
            propertyInfoVO.getCompanyLogo());
        log.info("更新物业信息：{}", propertyInfoVO);
        Result<Void> result = propertyInfoService.updateCurrentUserPropertyInfo(propertyInfoVO);
        log.info("更新物业信息成功");
        return result;
    }

    @GetMapping("/has-company")
    @Operation(summary = "检查物业公司关联", description = "检查当前用户是否有关联的物业公司")
    public Result<Boolean> hasPropertyCompany() throws Exception {
        log.info("检查用户是否有关联的物业公司");
        Long userId = SecurityUtils.getCurrentUserId();
        if (userId == null) {
            log.warn("用户未登录，无法检查物业公司关联");
            return Result.fail("用户未登录");
        }
        
        Result<Boolean> result = propertyInfoService.hasPropertyCompany();
        log.info("检查物业公司关联成功，用户ID：{}，结果：{}", userId, result.getData());
        return result;
    }

    @GetMapping("/stats")
    @Operation(summary = "获取物业信息统计", description = "获取物业信息相关统计")
    public Result<Map<String, Object>> getPropertyInfoStats() throws Exception {
        log.info("获取物业信息统计");
        Result<Map<String, Object>> result = propertyInfoService.getPropertyInfoStats();
        log.info("获取物业信息统计成功");
        return result;
    }

    @GetMapping("/stats/{propertyCompanyId}")
    @Operation(summary = "根据物业公司ID获取物业信息统计", description = "根据物业公司ID获取对应的物业信息统计")
    public Result<Map<String, Object>> getPropertyInfoStatsByCompanyId(
            @Parameter(description = "物业公司ID") @PathVariable Long propertyCompanyId) throws Exception {
        log.info("根据物业公司ID获取物业信息统计，物业公司ID：{}", propertyCompanyId);
        Result<Map<String, Object>> result = propertyInfoService.getPropertyInfoStats(propertyCompanyId);
        log.info("根据物业公司ID获取物业信息统计成功");
        return result;
    }

    @GetMapping("/export")
    @Operation(summary = "导出物业信息", description = "导出物业信息到Excel")
    public Result<String> exportPropertyInfo() throws Exception {
        log.info("导出物业信息");
        // 这里需要实现导出逻辑，暂时返回空字符串
        String filePath = "";
        log.info("导出物业信息成功，文件路径：{}", filePath);
        return Result.success("导出成功", filePath);
    }

    @DeleteMapping("/cache")
    @Operation(summary = "清除物业信息缓存", description = "清除物业信息相关缓存")
    public Result<Void> clearPropertyInfoCache() throws Exception {
        log.info("清除物业信息缓存");
        // 这里需要实现缓存清除逻辑
        log.info("清除物业信息缓存成功");
        return Result.success();
    }
}
